package com.unitd.frame.sso.web;

import com.unitd.frame.sso.common.config.SSOConfig;
import com.unitd.frame.sso.exception.SsoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @desc SsoFilter过滤器或者Spring的拦截器默认的读取sso配置信息的配置对象
 * @filename SSOConfigurer.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/14
 */
public class SSOConfigurer extends SSOConfig {

	protected static final Logger logger = LoggerFactory.getLogger(SSOConfigurer.class.getName());

	private static final String CONFIG_LOCATION_PARAM = "ssoConfigLocation"; 		// 指定sso配置文件的位置(sso.properties 配置文件的默认存放位置)
	private String ssoPropPath = "sso.properties"; 									// SSO的默认配置文件名称

	public SSOConfigurer() {
		/* 支持无参构造函数 */
	}

	public SSOConfigurer(String ssoPropPath) {
		this.ssoPropPath = ssoPropPath;
	}

	/**
	 * @desc web.xml 启动监听, 初始化
	 * @param servletContext
	 */
	public void initSso(ServletContext servletContext) {
		// 从监听器的配置参数中获取sso.properties的位置
		String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
		if (location != null) {
			if (location.indexOf("classpath") >= 0) {
				String[] cfg = location.split(":");
				if (cfg.length == 2) {
					this.initProperties(getInputStream(cfg[1]));
				}
			} else {
				File file = new File(location);
				if (file.isFile()) {
					try {
						this.initProperties(getInputStream(new FileInputStream(file)));
					} catch (FileNotFoundException e) {
						throw new SsoException(location, e);
					}
				} else {
					throw new SsoException(location);
				}
			}
		} else {
			servletContext.log("初始化ssoConfigLocation指定的类路径不可用!");
		}
	}

	/**
	 * @desc Spring bean 注入初始化
	 * xml 配置方法：
	 * <bean id="ssoInit" class="com.unitd.comm.sso.web.WebSsoConfigurer" init-method="initSso">
	 * 		<property name="ssoPropPath" value="sso.properties" />
	 * </bean>
	 */
	public void initSso() {
		Properties prop;

		/* 尝试文件读取 */
		File file = new File(this.getSsoPropPath());
		if (file.isFile()) {
			try {
				prop = getInputStream(new FileInputStream(file));
			} catch (FileNotFoundException e) {
				throw new SsoException(this.getSsoPropPath(), e);
			}
		} else {
			prop = getInputStream(this.getSsoPropPath());
		}

		/* 初始化 */
		if (prop != null) {
			this.initProperties(prop);
		} else {
			logger.error("初始化ssoConfigLocation指定的类路径不可用!");
		}
	}

	public void shutdownSso() {
		logger.info("销毁当前SSO ");
	}

	private Properties getInputStream(String cfg) {
		return getInputStream(SSOConfigurer.class.getClassLoader().getResourceAsStream(cfg));
	}

	private Properties getInputStream(InputStream in) {
		Properties p = null;
		try {
			p = new Properties();
			p.load(in);
		} catch (Exception e) {
			logger.error("读取sso配置文件出错:" + e.toString());
		}
		return p;
	}

	public String getSsoPropPath() {
		return ssoPropPath;
	}

	public void setSsoPropPath(String ssoPropPath) {
		this.ssoPropPath = ssoPropPath;
	}
}